#!/bin/bash

# RustDesk 完整部署编译脚本
# 使用方法: ./build_all.sh [server|desktop|mobile|all]

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_step() { echo -e "${BLUE}[STEP]${NC} $1"; }

# 检查依赖
check_dependencies() {
    log_step "检查编译依赖..."
    
    # 检查Rust
    if ! command -v cargo &> /dev/null; then
        log_error "Cargo未安装，请先安装Rust"
        exit 1
    fi
    
    # 检查Flutter（移动端需要）
    if [ "$1" = "mobile" ] || [ "$1" = "all" ]; then
        if ! command -v flutter &> /dev/null; then
            log_warn "Flutter未安装，将跳过移动端编译"
        fi
    fi
    
    log_info "依赖检查完成"
}

# 编译客户端命令行工具
build_cli() {
    log_step "编译RustDesk客户端命令行工具..."
    
    # 清理之前的编译结果
    # cargo clean
    
    # 编译客户端命令行工具（支持Linux和Windows）
    log_info "编译Linux客户端命令行工具..."
    cargo build --release --features "cli" --target x86_64-unknown-linux-gnu
    
    if command -v x86_64-w64-mingw32-gcc &> /dev/null; then
        log_info "编译Windows客户端命令行工具..."
        cargo build --release --features "cli" --target x86_64-pc-windows-gnu
    else
        log_warn "未找到MinGW，跳过Windows客户端命令行工具编译"
    fi
    
    log_info "客户端命令行工具编译完成"
}

# 编译服务端
build_server() {
    log_step "编译RustDesk服务端..."
    
    # 清理之前的编译结果
    # cargo clean
    
    # 编译服务端（支持Linux和Windows）
    log_info "编译Linux服务端..."
    cargo build --release --bin service --features "cli" --target x86_64-unknown-linux-gnu
    
    if command -v x86_64-w64-mingw32-gcc &> /dev/null; then
        log_info "编译Windows服务端..."
        cargo build --release --bin service --features "cli" --target x86_64-pc-windows-gnu
    else
        log_warn "未找到MinGW，跳过Windows服务端编译"
    fi
    
    log_info "服务端编译完成"
}

# 编译桌面客户端
build_desktop() {
    log_step "编译桌面客户端..."
    
    # 获取当前平台
    PLATFORM=$(uname -s)
    ARCH=$(uname -m)
    
    case $PLATFORM in
        Linux)
            log_info "编译Linux桌面客户端..."
            cargo build --release --features "use_dasp,unix-file-copy-paste,plugin_framework"
            
            # 编译命令行工具
            # log_info "编译Linux命令行工具..."
            # cargo build --release --features "cli,use_dasp"
            ;;
        Darwin)
            log_info "编译macOS桌面客户端..."
            if [ "$ARCH" = "arm64" ]; then
                cargo build --release --features "use_dasp,screencapturekit" --target aarch64-apple-darwin
            else
                cargo build --release --features "use_dasp,screencapturekit" --target x86_64-apple-darwin
            fi
            ;;
        MINGW*|CYGWIN*|MSYS*)
            log_info "编译Windows桌面客户端..."
            cargo build --release --features "use_dasp,hwcodec,plugin_framework"
            ;;
        *)
            log_warn "不支持的平台: $PLATFORM"
            ;;
    esac
    
    log_info "桌面客户端编译完成"
}

# 编译移动客户端
build_mobile() {
    log_step "编译移动客户端..."
    
    if ! command -v flutter &> /dev/null; then
        log_error "Flutter未安装，无法编译移动客户端"
        return 1
    fi
    
    cd flutter || { log_error "Flutter目录不存在"; return 1; }
    
    # 获取Flutter依赖
    log_info "获取Flutter依赖..."
    flutter pub get
    
    # 编译Android
    if command -v android &> /dev/null || [ -d "$ANDROID_HOME" ]; then
        log_info "编译Android APK..."
        flutter build apk --release
        log_info "Android APK编译完成: build/app/outputs/flutter-apk/app-release.apk"
    else
        log_warn "Android SDK未配置，跳过Android编译"
    fi
    
    # 编译iOS（仅在macOS上）
    if [ "$(uname -s)" = "Darwin" ]; then
        if command -v xcodebuild &> /dev/null; then
            log_info "编译iOS App..."
            flutter build ios --release --no-codesign
            log_info "iOS App编译完成"
        else
            log_warn "Xcode未安装，跳过iOS编译"
        fi
    fi
    
    cd ..
    log_info "移动客户端编译完成"
}

# 编译Web客户端
build_web() {
    log_step "编译Web客户端..."
    
    if ! command -v flutter &> /dev/null; then
        log_error "Flutter未安装，无法编译Web客户端"
        return 1
    fi
    
    cd flutter || { log_error "Flutter目录不存在"; return 1; }
    
    log_info "编译Web版本..."
    flutter build web --release
    
    cd ..
    log_info "Web客户端编译完成: flutter/build/web/"
}

# 打包部署文件
package_deployment() {
    local mode=$1
    log_step "打包部署文件 (模式: $mode)..."
    
    DEPLOY_DIR="rustdesk_deployment_$(date +%Y%m%d_%H%M%S)"
    mkdir -p "$DEPLOY_DIR"

    case $mode in
        cli|all)
            # 客户端命令行工具
            if [ -d "target/x86_64-unknown-linux-gnu/release" ]; then
                mkdir -p "$DEPLOY_DIR/cli/linux"
                cp target/x86_64-unknown-linux-gnu/release/rustdesk "$DEPLOY_DIR/cli/linux/rustdesk" 2>/dev/null || true
            fi

            if [ -d "target/x86_64-pc-windows-gnu/release" ]; then
                mkdir -p "$DEPLOY_DIR/cli/windows"
                cp target/x86_64-pc-windows-gnu/release/rustdesk.exe "$DEPLOY_DIR/cli/windows/rustdesk.exe" 2>/dev/null || true
            fi
            ;;&
        server|all)
            # 服务端文件
            if [ -d "target/x86_64-unknown-linux-gnu/release" ]; then
                mkdir -p "$DEPLOY_DIR/server/linux"
                cp target/x86_64-unknown-linux-gnu/release/service "$DEPLOY_DIR/server/linux/rustdesk-server" 2>/dev/null || true
            fi
            
            if [ -d "target/x86_64-pc-windows-gnu/release" ]; then
                mkdir -p "$DEPLOY_DIR/server/windows"
                cp target/x86_64-pc-windows-gnu/release/service.exe "$DEPLOY_DIR/server/windows/rustdesk-server.exe" 2>/dev/null || true
            fi
            ;;&
        desktop|all)
            # 桌面客户端
            mkdir -p "$DEPLOY_DIR/desktop"
            cp target/release/rustdesk "$DEPLOY_DIR/desktop/" 2>/dev/null || true
            cp target/release/rustdesk.exe "$DEPLOY_DIR/desktop/" 2>/dev/null || true
            ;;&
        mobile|all)
            # 移动客户端
            if [ -f "flutter/build/app/outputs/flutter-apk/app-release.apk" ]; then
                mkdir -p "$DEPLOY_DIR/mobile"
                cp flutter/build/app/outputs/flutter-apk/app-release.apk "$DEPLOY_DIR/mobile/rustdesk.apk"
            fi
            ;;&
        web|all)
            # Web客户端
            if [ -d "flutter/build/web" ]; then
                cp -r flutter/build/web "$DEPLOY_DIR/web"
            fi
            ;;
    esac
    
    # 创建部署说明
    cat > "$DEPLOY_DIR/README.md" << EOF
# RustDesk 部署包

## 客户端命令行工具
- 将 cli/ 目录下的文件分发给用户
- 支持 Linux 和 Windows 平台

## 服务端部署
1. 将 server/ 目录下的文件部署到服务器
2. 运行: ./rustdesk-server --help 查看帮助
3. 启动服务: ./rustdesk-server --service

## 桌面客户端
- 将 desktop/ 目录下的文件分发给用户
- 支持 GUI 和命令行模式

## 移动客户端
- Android: 安装 mobile/rustdesk.apk
- iOS: 需要通过App Store或企业证书分发

## Web客户端
- 将 web/ 目录部署到Web服务器
- 通过浏览器访问

编译时间: $(date)
EOF
    
    log_info "部署包已创建: $DEPLOY_DIR"
    log_info "请查看 $DEPLOY_DIR/README.md 获取部署说明"
}

# 主函数
main() {
    local mode=${1:-all}
    
    log_info "RustDesk 完整部署编译开始..."
    log_info "编译模式: $mode"
    
    check_dependencies "$mode"
    
    case $mode in
        cli)
            build_cli
            ;;
        server)
            build_server
            ;;
        desktop)
            build_desktop
            ;;
        mobile)
            build_mobile
            ;;
        web)
            build_web
            ;;
        all)
            build_cli
            build_server
            build_desktop
            build_mobile
            build_web
            ;;
        *)
            log_error "无效的编译模式: $mode"
            echo "使用方法: $0 [server|desktop|mobile|web|all]"
            exit 1
            ;;
    esac
    
    package_deployment "$mode"
    log_info "RustDesk 完整部署编译完成!"
}

# 执行主函数
main "$@" 